package com.insurance.modules.partner.controller;

import java.util.*;

import com.insurance.entity.PartnerRolePermissionEntity;
import com.insurance.service.PartnerRolePermissionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.insurance.common.utils.PageUtils;
import com.insurance.common.utils.R;



/**
 * 合作方角色权限表
 *
 * @date 2021-02-27 15:51:21
 */
@Slf4j
@RestController
@RequestMapping("partner/partnerrolepermission")
public class PartnerRolePermissionController {
    
    @Autowired
    private PartnerRolePermissionService partnerRolePermissionService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("partner:partnerrolepermission:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = partnerRolePermissionService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{rpId}")
    @RequiresPermissions("partner:partnerrolepermission:info")
    public R info(@PathVariable("rpId") String rpId){
		PartnerRolePermissionEntity partnerRolePermission = partnerRolePermissionService.getById(rpId);
        return R.ok().put("partnerRolePermission", partnerRolePermission);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("partner:partnerrolepermission:save")
    public R save(@RequestBody PartnerRolePermissionEntity partnerRolePermission){
        partnerRolePermission.setRpId(UUID.randomUUID().toString().replaceAll("-", ""));
		partnerRolePermissionService.save(partnerRolePermission);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("partner:partnerrolepermission:update")
    public R update(@RequestBody PartnerRolePermissionEntity partnerRolePermission){
		partnerRolePermissionService.updateById(partnerRolePermission);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("partner:partnerrolepermission:delete")
    public R delete(@RequestBody String[] rpIds){
		partnerRolePermissionService.removeByIds(Arrays.asList(rpIds));
        return R.ok();
    }

    @RequestMapping("/configPermission")
    public R configPermission(String roleId, String permissionIdList){
        try {
            if(StringUtils.isBlank(roleId)){
                return R.error();
            }
            List<PartnerRolePermissionEntity> permissionEntityList = new ArrayList<PartnerRolePermissionEntity>();
            if(!StringUtils.isBlank(permissionIdList)){
                String[] permissionIds = permissionIdList.split(",");
                if(permissionIds != null && permissionIds.length > 0){
                    for(String permissionId : permissionIds){
                        permissionEntityList.add(new PartnerRolePermissionEntity(roleId, permissionId));
                    }
                }
            }
            boolean flag = partnerRolePermissionService.saveAndDelete(roleId, permissionEntityList);
            return flag ? R.ok() : R.error();
        } catch (Exception e) {
            log.error("【保存角色权限失败, 系统异常】", e);
            return R.error();
        }
    }

    
    
}
